package com.hy.admin.service.impl;

import com.hy.admin.dao.YangCardDao;
import com.hy.admin.dao.YangCardOrderDao;
import com.hy.admin.entity.YangCardEntity;
import com.hy.admin.entity.YangCardOrderEntity;
import com.hy.admin.enums.EnumPayState;
import com.hy.admin.enums.EnumStatus;
import com.hy.admin.helper.response.OrderResponse;
import com.hy.admin.service.YangCardOrderService;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.UUID;

@Service
public class YangCardOrderServiceImpl implements YangCardOrderService {
    @Autowired
    private YangCardDao yangCardDao;
    @Autowired
    private YangCardOrderDao yangCardOrderDao;

    @Override
    public ServicesResult<OrderResponse> buy(Long memberId, Long cardId) {
        ServicesResult<OrderResponse> servicesResult = new ServicesResult<>();
        //校验请求参数
        if (cardId == null || cardId < 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        if (memberId == null || memberId < 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        //查询氧卡
        YangCardEntity yangCardEntity = yangCardDao.selectByPrimaryKey(cardId);
        if (yangCardEntity == null){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求参数有误");
            return servicesResult;
        }
        //创建订单对象
        YangCardOrderEntity ycoEntity = new YangCardOrderEntity();

        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();

        ycoEntity.setUuid(uuid);
        ycoEntity.setCardId(cardId);
        ycoEntity.setMemberId(memberId);
        ycoEntity.setPayState(EnumPayState.UNPAID.getId());
        ycoEntity.setPrice(yangCardEntity.getPrice());
        ycoEntity.setStatus(EnumStatus.VALID.getId());
        Calendar cal = Calendar.getInstance();
        ycoEntity.setCreateTime(cal.getTime());
        ycoEntity.setUpdateTime(cal.getTime());

        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_YEAR, 1);
        //设置过期时间
        cal.add(Calendar.MONTH, yangCardEntity.getValidTime());

        ycoEntity.setEndTime(cal.getTime());
        //插入数据
        int result = yangCardOrderDao.insertSelective(ycoEntity);
        if(result <= 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("数据插入失败");
            return servicesResult;
        }
        Long id = yangCardOrderDao.selectByUuid(uuid);
        //返回结果
        OrderResponse orderResponse = new OrderResponse();
        orderResponse.setId(id);
        orderResponse.setResult(true);
        servicesResult.setResult(orderResponse);
        return servicesResult;
    }

    @Override
    public Boolean cardExpire() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        int count = yangCardOrderDao.selectExpire(cal.getTime());
        if( count > 0 ){
            int result = yangCardOrderDao.cardExpire(cal.getTime());
            return result > 0;
        }

        return true;
    }

}
